package show.marvelous.domain.chessgame.repository;

import java.util.Collection;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import show.marvelous.domain.chessgame.aggregate.entity.MoveEvent;
import show.marvelous.domain.chessgame.aggregate.entity.MoveEventBuilder;
import show.marvelous.infrastructure.dao.chessgame.IMoveEventDao;
import show.marvelous.infrastructure.dao.chessgame.MoveEventDO;
import show.marvelous.infrastructure.dao.chessgame.MoveEventDOBuilder;

/**
 * The type Move event repository.
 * @author Kris 2021/8/24
 */
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MoveEventRepository{

    private final IMoveEventDao moveEventDao;

    /**
     * Save.
     *
     * @param moveEvent the move event
     */
    public void save(long gameId, MoveEvent moveEvent){
        MoveEventDO moveEventDO = MoveEventDOBuilder.moveEventDO().id(moveEvent.getId()).gameId(gameId)
                .type(moveEvent.getType()).piece(moveEvent.getPiece()).positionColor(moveEvent.getPositionColor())
                .positionIndex(moveEvent.getPositionIndex()).build();
        moveEventDao.save(moveEventDO);
    }

    public Collection<MoveEvent> listMoveEvents(long gameId){
        return moveEventDao.listByGameId(gameId).stream()
                .map(e -> MoveEventBuilder.moveEvent().id(e.getId()).type(e.getType()).piece(e.getPiece())
                        .positionColor(e.getPositionColor()).positionIndex(e.getPositionIndex()).build())
                .collect(Collectors.toList());
    }
}
